﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using HotDealD2.Models;

namespace HotDealD2.Helper
{
    public class CustomRoleProvider: RoleProvider   
    {
        //private QLDataBaseDataContext dataContext;
        //public CustomRoleProvider()
        //{
        //    dataContext = new QLDataBaseDataContext();
        //    var data = (from d in dataContext.LoaiThanhViens
        //                select d).ToList();

        //    foreach (LoaiThanhVien tv in data)
        //    {
        //        CreateRole(tv.ThanhVien);
        //    }
        //}

        public override string[] GetRolesForUser(string username)
        {
            QLDataBaseDataContext dataContext = new QLDataBaseDataContext();

            var data = (from d in dataContext.ThanhViens
                        where d.Username == username
                        select d).ToList();

            if (data.Count > 0)
            {
                return new string[] { data[0].LoaiThanhVien.ThanhVien };
            }
            else
            {
                return new String[] { };
            }
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            QLDataBaseDataContext dataContext = new QLDataBaseDataContext();

            var data = (from d in dataContext.ThanhViens
                        where d.Username == username
                        select d).ToList();
            if (data.Count > 0)
            {
                if (data[0].LoaiThanhVien.ThanhVien.Equals(roleName))
                {
                    return true;
                }
            }
            return false;
        }
        public override string[] GetAllRoles()
        {
            QLDataBaseDataContext dataContext = new QLDataBaseDataContext();

            var ltvs = dataContext.LoaiThanhViens.Select(row => row);

            List<string> listRole = new List<string>();
            foreach (var loaitv in ltvs)
            {
                listRole.Add(loaitv.ThanhVien);
            }
            return listRole.ToArray();
        }
        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }
        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }
        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }
        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }
        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }
        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }
    }

}